/**
  ******************************************************************************
  * @file           : usbd_custom_hid_if.c
  * @brief          : USB Device Custom HID interface file.
  ******************************************************************************
  * This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * Copyright (c) 2017 STMicroelectronics International N.V.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice,
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other
  *    contributors to this software may be used to endorse or promote products
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under
  *    this license is void and will automatically terminate your rights under
  *    this license.
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include "usbd_custom_hid_if.h"
/* USER CODE BEGIN INCLUDE */
/* USER CODE END INCLUDE */
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  * @{
  */

/** @defgroup USBD_CUSTOM_HID
  * @brief usbd core module
  * @{
  */

/** @defgroup USBD_CUSTOM_HID_Private_TypesDefinitions
  * @{
  */
/* USER CODE BEGIN PRIVATE_TYPES */
/* USER CODE END PRIVATE_TYPES */
/**
  * @}
  */

/** @defgroup USBD_CUSTOM_HID_Private_Defines
  * @{
  */
/* USER CODE BEGIN PRIVATE_DEFINES */
/* USER CODE END PRIVATE_DEFINES */

/**
  * @}
  */

/** @defgroup USBD_CUSTOM_HID_Private_Macros
  * @{
  */
/* USER CODE BEGIN PRIVATE_MACRO */
/* USER CODE END PRIVATE_MACRO */

/**
  * @}
  */

/** @defgroup USBD_AUDIO_IF_Private_Variables
 * @{
 */
//__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
//{
//  /* USER CODE BEGIN 0 */
//  0x00,
//  /* USER CODE END 0 */
//  0xC0    /*     END_COLLECTION	             */
//
//};
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS [USBD_CUSTOM_HID_REPORT_DESC_SIZE]
__ALIGN_END =
{
    0x06, 0xFF, 0x00, /* USAGE_PAGE (Vendor Page: 0xFF00) */
    0x09, 0x01, /* USAGE (Demo Kit) */
    0xa1, 0x01, /* COLLECTION (Application) */
    /* 6 */
    /* LED1 */
    0x85, LED1_REPORT_ID, /* REPORT_ID (1) */
    0x09, 0x01, /* USAGE (LED 1) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
    0x75, 0x08, /* REPORT_SIZE (8) */
    0x95, LED1_REPORT_COUNT, /* REPORT_COUNT (1) */
    0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    0x85, LED1_REPORT_ID, /* REPORT_ID (1) */
    0x09, 0x01, /* USAGE (LED 1) */
    0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
    /* 26 */
    /* LED2 */
    0x85, LED2_REPORT_ID, /* REPORT_ID 2 */
    0x09, 0x02, /* USAGE (LED 2) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
    0x75, 0x08, /* REPORT_SIZE (8) */
    0x95, LED2_REPORT_COUNT, /* REPORT_COUNT (1) */
    0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    0x85, LED2_REPORT_ID, /* REPORT_ID (2) */
    0x09, 0x02, /* USAGE (LED 2) */
    0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
    /* 46 */
    /* LED3 */
    0x85, LED3_REPORT_ID, /* REPORT_ID (3) */
    0x09, 0x03, /* USAGE (LED 3) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
    0x75, 0x08, /* REPORT_SIZE (8) */
    0x95, LED3_REPORT_COUNT, /* REPORT_COUNT (1) */
    0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    0x85, LED3_REPORT_ID, /* REPORT_ID (3) */
    0x09, 0x03, /* USAGE (LED 3) */
    0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) *//* 66 */
    /* LED4 */
    0x85, LED4_REPORT_ID, /* REPORT_ID 4) */
    0x09, 0x04, /* USAGE (LED 4) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
    0x75, 0x08, /* REPORT_SIZE (8) */
    0x95, LED4_REPORT_COUNT, /* REPORT_COUNT (1) */
    0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    0x85, LED4_REPORT_ID, /* REPORT_ID (4) */
    0x09, 0x04, /* USAGE (LED 4) */
    0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
    /* 86 */
    /* key Push Button */
    0x85, KEY_REPORT_ID, /* REPORT_ID (5) */
    0x09, 0x05, /* USAGE (Push Button) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
    0x75, 0x01, /* REPORT_SIZE (1) */
    0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
    0x09, 0x05, /* USAGE (Push Button) */
    0x75, 0x01, /* REPORT_SIZE (1) */
    0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    0x75, 0x07, /* REPORT_SIZE (7) */
    0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
    0x85, KEY_REPORT_ID, /* REPORT_ID (2) */
    0x75, 0x07, /* REPORT_SIZE (7) */
    0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
    /* 114 */
    /* Tamper Push Button */
    0x85, TAMPER_REPORT_ID,/* REPORT_ID (6) */
    0x09, 0x06, /* USAGE (Tamper Push Button) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
    0x75, 0x01, /* REPORT_SIZE (1) */
    0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
    0x09, 0x06, /* USAGE (Tamper Push Button) */
    0x75, 0x01, /* REPORT_SIZE (1) */
    0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    0x75, 0x07, /* REPORT_SIZE (7) */0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
    0x85, TAMPER_REPORT_ID,/* REPORT_ID (6) */
    0x75, 0x07, /* REPORT_SIZE (7) */
    0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
    /* 142 */
    /* ADC IN */
    0x85, ADC_REPORT_ID, /* REPORT_ID */
    0x09, 0x07, /* USAGE (ADC IN) */
    0x15, 0x00, /* LOGICAL_MINIMUM (0) */
    0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
    0x75, 0x08, /* REPORT_SIZE (8) */
    0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
    0x85, ADC_REPORT_ID, /* REPORT_ID (7) */
    0x09, 0x07, /* USAGE (ADC in) */
    0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
    /* 161 */
    0xc0 /* END_COLLECTION */
};

/* USER CODE BEGIN PRIVATE_VARIABLES */
/* USER CODE END PRIVATE_VARIABLES */
/**
  * @}
  */

/** @defgroup USBD_CUSTOM_HID_IF_Exported_Variables
  * @{
  */
extern USBD_HandleTypeDef hUsbDeviceFS;
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */

/**
  * @}
  */

/** @defgroup USBD_CUSTOM_HID_Private_FunctionPrototypes
  * @{
  */
static int8_t CUSTOM_HID_Init_FS     (void);
static int8_t CUSTOM_HID_DeInit_FS   (void);
static int8_t CUSTOM_HID_OutEvent_FS (uint8_t event_idx, uint8_t state);


USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops_FS =
{
    CUSTOM_HID_ReportDesc_FS,
    CUSTOM_HID_Init_FS,
    CUSTOM_HID_DeInit_FS,
    CUSTOM_HID_OutEvent_FS,
};

/* Private functions ---------------------------------------------------------*/
/**
  * @brief  CUSTOM_HID_Init_FS
  *         Initializes the CUSTOM HID media low layer
  * @param  None
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  */
static int8_t CUSTOM_HID_Init_FS(void)
{
    /* USER CODE BEGIN 4 */
    return (0);
    /* USER CODE END 4 */
}

/**
  * @brief  CUSTOM_HID_DeInit_FS
  *         DeInitializes the CUSTOM HID media low layer
  * @param  None
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  */
static int8_t CUSTOM_HID_DeInit_FS(void)
{
    /* USER CODE BEGIN 5 */
    return (0);
    /* USER CODE END 5 */
}

/**
  * @brief  CUSTOM_HID_OutEvent_FS
  *         Manage the CUSTOM HID class events
  * @param  event_idx: event index
  * @param  state: event state
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  */
static int8_t CUSTOM_HID_OutEvent_FS  (uint8_t event_idx, uint8_t state)
{
    /* USER CODE BEGIN 6 */
    switch(event_idx)
    {
        case 1: /* LED3 */
//            (state == 1) ? HAL_GPIO_WritePin(LD3_GPIO_Port,LD3_Pin,GPIO_PIN_SET) :
//            HAL_GPIO_WritePin(LD3_GPIO_Port,LD3_Pin,GPIO_PIN_RESET);
            printf("event_idx = 1\r\n");
            break;
        case 2: /* LED4 */
//            (state == 1) ? HAL_GPIO_WritePin(LD4_GPIO_Port,LD4_Pin,GPIO_PIN_SET) :
//            HAL_GPIO_WritePin(LD4_GPIO_Port,LD4_Pin,GPIO_PIN_RESET);
            printf("event_idx = 2\r\n");
            break;
        case 3: /* LED5 */
//            (state == 1) ? HAL_GPIO_WritePin(LD5_GPIO_Port,LD5_Pin,GPIO_PIN_SET) :
//            HAL_GPIO_WritePin(LD5_GPIO_Port,LD5_Pin,GPIO_PIN_RESET);
            printf("event_idx = 3\r\n");
            break;
        case 4: /* LED6 */
//            (state == 1) ? HAL_GPIO_WritePin(LD6_GPIO_Port,LD6_Pin,GPIO_PIN_SET) :
//            HAL_GPIO_WritePin(LD6_GPIO_Port,LD6_Pin,GPIO_PIN_RESET);
            printf("event_idx = 4\r\n");
            break;
        default:
            break;
    }
    return (0);
    /* USER CODE END 6 */
    //  return (0);
}

/* USER CODE BEGIN 7 */
/**
  * @brief  USBD_CUSTOM_HID_SendReport_FS
  *         Send the report to the Host
  * @param  report: the report to be sent
  * @param  len: the report length
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  */
/*
static int8_t USBD_CUSTOM_HID_SendReport_FS ( uint8_t *report,uint16_t len)
{
  return USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, len);
}
*/
/* USER CODE END 7 */

/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */

/**
  * @}
  */

/**
  * @}
  */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
